package org.xa3apm.vote.hibernate.bean;

import java.io.Serializable;
import java.util.Collection;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.xa3apm.vote.hibernate.entities.Municipality;
import org.xa3apm.vote.hibernate.entities.Settlement;

@Stateless
public class SettlementBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8760061300563895248L;
	@PersistenceContext(unitName = "vote")
	private EntityManager em;

	public Settlement getById(Long id) {
		return (Settlement) (em.createNamedQuery("Settlement.getById")
				.setParameter("id", id).getSingleResult());
	}

	public Collection<Settlement> getAll() {
		@SuppressWarnings("unchecked")
		Collection<Settlement> resultList = em.createNamedQuery(
				"Settlement.getAll").getResultList();
		return resultList;
	}

	public Collection<Settlement> getByMunicipalityId(Municipality municipality) {
		@SuppressWarnings("unchecked")
		Collection<Settlement> resultList = em
				.createNamedQuery("Settlement.getByMunicipalityId")
				.setParameter("municipality", municipality).getResultList();
		return resultList;
	}

}
